<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.*,java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.springframework.web.context.*"%>
<%@ page import="org.springframework.web.context.support.*"%>
<%@ page import="aip.core.model.attachment.Photo"%>
<%@ page import="org.springframework.web.multipart.*"%>
<%@ page import="aip.ui.attachment.util.ThumbnailUtils"%>
<%@ page import="java.awt.image.BufferedImage"%>
<%@ page import="javax.imageio.ImageIO"%>
<%@ page import="java.io.ByteArrayOutputStream"%>
<%@ page import="java.io.ByteArrayInputStream"%>
<%@ page import="java.io.InputStream"%>
<%@ page import="aip.ui.user.util.UserHelper"%>
<%@ page import="aip.ui.attachment.srvc.AttachmentService"%>
<%
	WebApplicationContext ctx = WebApplicationContextUtils
			.getWebApplicationContext(request.getSession()
					.getServletContext());
	AttachmentService attachmentService = (AttachmentService) ctx
			.getBean("attachmentService");
	Photo photo = new Photo();
	//错误信息
	String errorMsg = "";

	//图片设置信息
	String id = "";
	String imgTitle = "";
	String imgWidth = "";
	String imgHeight = "";
	String imgBorder = "";
	String phtoId = "";

	//文件最大值
	long maxSize = 1000000;

	//定义文件上传的类型
	String[] types = new String[] { ".gif", ".jpg", ".jpeg", ".png", ".bmp" };

	//文件保存目录URL
	String saveUrl = "../commons/showImg.shtml?atchType=PHOTO&id=";

	//判断是否为单个文件
	boolean isMultipart = ServletFileUpload.isMultipartContent(request);
	if (isMultipart) {
		FileItemFactory factory = new DiskFileItemFactory();

		ServletFileUpload upload = new ServletFileUpload(factory);

		List items = upload.parseRequest(request);

		// Process the uploaded items
		Iterator iter = items.iterator();
		while (iter.hasNext()) {
			FileItem item = (FileItem) iter.next();
			//原文件名
			String fileName = item.getName();

			if (item.getFieldName().equals("id")) {
				id = item.getString();
			}
			if (item.getFieldName().equals("imgTitle")) {
				imgTitle = item.getString();
			}
			if (item.getFieldName().equals("imgWidth")) {
				imgWidth = item.getString();
			}
			if (item.getFieldName().equals("imgHeight")) {
				imgHeight = item.getString();
			}
			if (item.getFieldName().equals("imgBorder")) {
				imgBorder = item.getString();
			}

			if (!item.isFormField()) {

				//检查文件名
				if (item.getName() == "" || item.getString() == null) {
					errorMsg = "请选择文件。";
					break;
				}

				//检查文件大小
				if (item.getSize() > maxSize) {
					errorMsg = "上传文件大小超过限制。";
					break;
				}

				//获得文件扩展名
				String fileExt = fileName.substring(fileName
						.indexOf("."));
				String tempName = fileName.substring(fileName
						.lastIndexOf("\\") + 1, fileName.indexOf("."));
				if (!Arrays.<String> asList(types).contains(
						fileExt.toLowerCase())) {
					errorMsg = "上传文件扩展名是不允许的扩展名。";
					break;
				}

				// 缩略图
				int width = 320, height = 240;
				BufferedImage srcImage;
				String imgType = "JPEG";
				srcImage = ImageIO.read(item.getInputStream());
				if (width > 0 || height > 0) {
					srcImage = ThumbnailUtils.resize(srcImage, width,
							height);
				}
				ByteArrayOutputStream thumbnail = new ByteArrayOutputStream();
				ImageIO.write(srcImage, imgType, thumbnail);
				InputStream is = new ByteArrayInputStream(thumbnail
						.toByteArray());

				// 通用附件可能存在大文件,防止outofmemory发生,统一使用inputStream
				photo.setContent(item.getInputStream());
				photo.setType("PHOTO");
				photo.setCreator(UserHelper.getUser().getUsername());
				photo.setCreate(new Date());
				photo.setName(tempName);
				photo.setExtension(fileExt);
				photo.setTitle(imgTitle);
				photo
						.setLink("/commons/showImg.shtml?atchType=PHOTO&id=");
				photo.setOrder(1);
				photo.setPhotoContent(imgTitle);
				photo.setThumbnail(is);

				attachmentService.save(photo);
				phtoId = String.valueOf(photo.getId());
				saveUrl = saveUrl.concat(phtoId);
				request.getSession().setAttribute("test",
						String.valueOf(photo.getId()));
			}

		}
		if (errorMsg.equals("")) {
			//插入图片，关闭层
			response.setHeader("Content-Type",
					"text/html; charset=UTF-8");
			response.getWriter().append(
					"{\"error\":0,\"url\":\"" + saveUrl
							+ "\",\"photoId\":\"" + phtoId + "\"}");
		} else {
			response.setHeader("Content-Type",
					"text/html; charset=UTF-8");
			response.getWriter().append(
					"{\"error\":1,\"message\":\"" + errorMsg + "\"}");
		}
	}
%>


